#include "win32GraphicsSubsystem.h"
#include "win32EventListener.h"
#include "win32GLWindow.h"
#include "neiderra/utils/logger.h"
#include "neiderra/core/ResourceManager.h"
#include "neiderra/core/io/IOStream.h"
#include "neiderra/gui/EventManager.h"
#include "neiderra/gui/EventDispatcher.h"

using namespace neiderra::core;
using namespace neiderra::gui;


win32GraphicsSubsystem::win32GraphicsSubsystem(Core* core):
	iGraphicsSubsystem(core)
{
	_eventListener = new win32EventListener(core);
	_eventDispatcher = new EventDispatcher(core);
	_eventManager = new EventManager(core);
}

win32GraphicsSubsystem::~win32GraphicsSubsystem()
{
	// fixme: delete windows
/*	map<Window, GUIWindow*>::iterator i = _windows.begin();
	
	for(;i!=_windows.end();i++)
	{
		if(i->second != 0)
		{
			GUIWindow* win = i->second;
			delete win;
		}
	}*/
	
	delete _eventListener;
	delete _eventDispatcher;
	delete _eventManager;
	
	// fixme
//	delete _renderer; 
}

void win32GraphicsSubsystem::update(double deltaT)
{
	_eventListener->listen();
}

GUIWindow* win32GraphicsSubsystem::createSystemWindow(const std::string& title)
{
    GUIWindow* win = new win32GLWindow(core, title);
    _windows.push_back(win);

	// tip: render target make current
//    renderer_->init(win->getWindowImpl());
	return win;
}

//void win32GraphicsSubsystem::makeCurrentWindow(GUIWindow* win)
//{
//	//renderer_->init(win->getWindowImpl());
//}

GUIWindow* win32GraphicsSubsystem::createDefaultWindow(const std::string& title)
{
	GUIWindow* win = new win32GLWindow(core, title);
	//renderer_->init(win->getWindowImpl());
	return win;
}

GUIWindow* win32GraphicsSubsystem::createSystemWindow(
    const std::string& title,
    int x, int y, int width, int height,
    GUIWindow* parent)
{
    GUIWindow* win = new win32GLWindow(core, title, x, y, width, height, parent);
    _windows.push_back(win);
//    renderer_->init(win->getWindowImpl());
    return win;
}

